package com.sinopac.action.RptQuery;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.RptQuery.CompetentRecordVO;
import com.sinopac.entity.RptQuery.EmployeeRecordVO;
import com.sinopac.entity.RptQuery.MoratoriumListVO;
import com.sinopac.entity.RptQuery.PersonalRecordVO;
import com.sinopac.entity.RptQuery.TokenListVO;
import com.sinopac.entity.RptQuery.UnusualReportVO;
import com.sinopac.model.RptQuery.RptQueryModel;

@Namespace("/RptQuery")
@ParentPackage("struts-default")
public class RptQueryAction extends ActionCore {
	private static final long serialVersionUID = 1L;
	private List<EmployeeRecordVO> epyList;
	private List<CompetentRecordVO> cptList;
	private List<TokenListVO> tokenList;
	private List<MoratoriumListVO> mrtiList;
	private List<UnusualReportVO> uusList;
	private List<CompetentRecordVO> psrList;
	private List<PersonalRecordVO>jqList;
	private List<String> countList;
	

	private String count_sum;
	private String count_1;
	private String count_2;
	private String count_3;
	
	private static final String REPORT_1 = "report_1";
	private static final String REPORT_2 = "report_2";
	private static final String REPORT_3 = "report_3";
	private static final String REPORT_4 = "report_4";
	private static final String REPORT_5 = "report_5";
	private static final String REPORT_6 = "report_6";

	private InputStream fileInputStream;
	
	private String fileName;
	@Action(value = "index", results = {
			@Result(name = "report_1", location = "/form/RptQuery/EmployeeRecord.jsp"),
			@Result(name = "report_2", location = "/form/RptQuery/CompetentRecord.jsp"),
			@Result(name = "report_3", location = "/form/RptQuery/TokenList.jsp"),
			@Result(name = "report_4", location = "/form/RptQuery/MoratoriumList.jsp"),
			@Result(name = "report_5", location = "/form/RptQuery/UnusualReport.jsp"),
			@Result(name = "report_6", location = "/form/RptQuery/PersonalRecord.jsp")})
	public String index() {
//		System.out.println("to_i"+(String)session.get("to_i"));
		session.put("pdfPath", "");
		RptQueryModel rqm = new RptQueryModel();
		if("查詢行員作業紀錄".equals((String)session.get("sort"))){
			epyList = rqm.getEmployeeRecord((String)session.get("from_i"),(String)session.get("to_i"));
			return REPORT_1;
		}else if ("查詢主管覆核紀錄".equals((String)session.get("sort"))) {
			cptList = rqm.getCompetentRecord((String)session.get("from_i"),(String)session.get("to_i"));
			return REPORT_2;
		}else if ("查詢TOKEN庫存清單".equals((String)session.get("sort"))) {
			tokenList = rqm.getTokenList((String)session.get("from_i"),(String)session.get("to_i"));
			countList = rqm.getCount((String)session.get("from_i"),(String)session.get("to_i"));
			count_sum = countList.get(0);
			count_1 = countList.get(1);
			count_2 = countList.get(2);
			count_3 = countList.get(3);
			return REPORT_3;
		}else if ("查詢驗證失敗被暫禁清單".equals((String)session.get("sort"))) {
			mrtiList = rqm.getMoratoriumList((String)session.get("from_i"),(String)session.get("to_i"));
			return REPORT_4;
		}else if ("查詢異常報表".equals((String)session.get("sort"))) {
			uusList = rqm.getUnusualReport((String)session.get("from_i"),(String)session.get("to_i"));
			return REPORT_5;
		}else{
			psrList = rqm.getPersonalRecord((String)session.get("userName"));
			jqList = rqm.getJobQueuesRecord((String)session.get("userName"));
			return REPORT_6;
		}
	}
	
//	@Action(value = "personal", results = {
//			@Result(name = "report_6", location = "/form/PersonalRecord/PersonalRecord.jsp"),
//	})
//	public String personal(){
//		RptQueryModel rqm = new RptQueryModel();
//		psrList = rqm.getPersonalRecord((String)session.get("userName"));
//		return REPORT_6;
//	}
	
//	@Action(value = "exportExcel", results = {
//			@Result(name = "excel_1", location = "/form/RptQuery/EmployeeRecord.jsp"),
//			@Result(name = "excel_2", location = "/form/RptQuery/CompetentRecord.jsp"),
//			@Result(name = "excel_3", location = "/form/RptQuery/TokenList.jsp"),
//			@Result(name = "excel_4", location = "/form/RptQuery/MoratoriumList.jsp"),
//			@Result(name = "excel_5", location = "/form/RptQuery/UnusualReport.jsp"),
//			@Result(name = "excel_6", location = "/form/RptQuery/PersonalRecord.jsp")})
	@Action(value="exportExcel", results={@Result(name="success", type="stream", 
								params = {"contentType", "application/vnd.ms-excel;charset=ISO8859-1",
    									  "inputName", "fileInputStream",
    									  "contentDisposition", "filename=\"${fileName}\"",
    									  "bufferSize", "4096"})})
	public String exportExcel(){
		RptQueryModel rqm = new RptQueryModel();
		fileName = "record.xls";
		if("查詢行員作業紀錄".equals((String)session.get("sort"))){
			epyList = rqm.getEmployeeRecord((String)session.get("from_i"),(String)session.get("to_i"));
			try {
				File file = rqm.exportEmployeeRecord(epyList, (String)session.get("sort"));
				fileInputStream = new FileInputStream(file);
				file.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}else if ("查詢主管覆核紀錄".equals((String)session.get("sort"))) {
			cptList = rqm.getCompetentRecord((String)session.get("from_i"),(String)session.get("to_i"));
			try {
				File file = rqm.exportCompetentRecord(cptList, (String)session.get("sort"));
				fileInputStream = new FileInputStream(file);
				file.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}else if ("查詢TOKEN庫存清單".equals((String)session.get("sort"))) {
			tokenList = rqm.getTokenList((String)session.get("from_i"),(String)session.get("to_i"));
			countList = rqm.getCount((String)session.get("from_i"),(String)session.get("to_i"));
			count_sum = countList.get(0);
			count_1 = countList.get(1);
			count_2 = countList.get(2);
			count_3 = countList.get(3);
			try {
				File file = rqm.exportTokenList(tokenList, (String)session.get("sort"));
				fileInputStream = new FileInputStream(file);
				file.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}else if ("查詢驗證失敗被暫禁清單".equals((String)session.get("sort"))) {
			mrtiList = rqm.getMoratoriumList((String)session.get("from_i"),(String)session.get("to_i"));
			try {
				File file = rqm.exportMoratoriumList(mrtiList, (String)session.get("sort"));
				fileInputStream = new FileInputStream(file);
				file.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}else if ("查詢異常報表".equals((String)session.get("sort"))) {
			uusList = rqm.getUnusualReport((String)session.get("from_i"),(String)session.get("to_i"));
			try {
				File file = rqm.exportUnusualReport(uusList, (String)session.get("sort"));
				fileInputStream = new FileInputStream(file);
				file.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}else{
			psrList = rqm.getPersonalRecord((String)session.get("userName"));
			jqList = rqm.getJobQueuesRecord((String)session.get("userName"));
			try {
				File file = rqm.exportPersonalRecord(psrList, jqList , (String)session.get("userName"));
				fileInputStream = new FileInputStream(file);
				file.delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}
	}

	public List<EmployeeRecordVO> getEpyList() {
		return epyList;
	}

	public void setEpyList(List<EmployeeRecordVO> epyList) {
		this.epyList = epyList;
	}

	public List<CompetentRecordVO> getCptList() {
		return cptList;
	}

	public void setCptList(List<CompetentRecordVO> cptList) {
		this.cptList = cptList;
	}

	public List<TokenListVO> getTokenList() {
		return tokenList;
	}

	public void setTokenList(List<TokenListVO> tokenList) {
		this.tokenList = tokenList;
	}

	public List<MoratoriumListVO> getMrtiList() {
		return mrtiList;
	}

	public void setMrtiList(List<MoratoriumListVO> mrtiList) {
		this.mrtiList = mrtiList;
	}

	public List<UnusualReportVO> getUusList() {
		return uusList;
	}
	
	public void setUusList(List<UnusualReportVO> uusList) {
		this.uusList = uusList;
	}
	
	public List<String> getCountList() {
		return countList;
	}

	public void setCountList(List<String> countList) {
		this.countList = countList;
	}

	public List<CompetentRecordVO> getPsrList() {
		return psrList;
	}

	public void setPsrList(List<CompetentRecordVO> psrList) {
		this.psrList = psrList;
	}	

	public List<PersonalRecordVO> getJqList() {
		return jqList;
	}

	public void setJqList(List<PersonalRecordVO> jqList) {
		this.jqList = jqList;
	}

	public String getCount_sum() {
		return count_sum;
	}

	public void setCount_sum(String count_sum) {
		this.count_sum = count_sum;
	}

	public String getCount_1() {
		return count_1;
	}

	public void setCount_1(String count_1) {
		this.count_1 = count_1;
	}

	public String getCount_2() {
		return count_2;
	}

	public void setCount_2(String count_2) {
		this.count_2 = count_2;
	}

	public String getCount_3() {
		return count_3;
	}

	public void setCount_3(String count_3) {
		this.count_3 = count_3;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getFileInputStream() {
		return fileInputStream;
	}

	public void setFileInputStream(InputStream fileInputStream) {
		this.fileInputStream = fileInputStream;
	}

}
